package com.itheima.app.manager.smsManager;


import cn.hutool.core.convert.Convert;
import com.itheima.commons.PageBeanResult;

import com.itheima.domain.db.sms.SmsHomeRecommendSubject;
import com.itheima.service.smsService.SmsHomeRecommendSubjectService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SmsHomeRecommendSubjectManager {

    @DubboReference
    private SmsHomeRecommendSubjectService smsHomeRecommendSubjectService;

    //分页查询推荐
    public PageBeanResult listBy(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {

        //创建返回类
        PageBeanResult<SmsHomeRecommendSubject> pageBean = new PageBeanResult<>();

        //查询总记录数
        Integer i = smsHomeRecommendSubjectService.count();

        //根据分页条件查询数据
        List<SmsHomeRecommendSubject> lists = smsHomeRecommendSubjectService.list(pageNum, pageSize, subjectName, recommendStatus);

        //封装数据到返回类
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        pageBean.setList(lists);
        pageBean.setTotal(i);
        if (i % pageSize == 0) {
            pageBean.setTotalPage(i / pageSize);
        }
        if (i % pageSize != 0) {
            pageBean.setTotalPage((i / pageSize) + 1);
        }
        return pageBean;
    }

    //添加首页推荐专题
    public void create(SmsHomeRecommendSubject smsHomeRecommendSubject) {
        smsHomeRecommendSubjectService.create(smsHomeRecommendSubject);
    }

    //批量删除专题推荐推荐
    public void deleteids(Integer[] ids) {
        //遍历IDS
        for (Integer id : ids) {
            //调用方法删除专题推荐
            smsHomeRecommendSubjectService.deleteByid(id);
        }
    }

    //批量修改推荐状态
    public void update(Integer[] ids, Integer recommendStatus) {
        //遍历ids
        for (Integer id : ids) {
            //封装到实体类
            SmsHomeRecommendSubject smsHomeRecommendSubject = new SmsHomeRecommendSubject();
            smsHomeRecommendSubject.setId(Convert.toLong(id));
            smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
            //调用方法修改状态
            smsHomeRecommendSubjectService.update(smsHomeRecommendSubject);
        }
    }

    //修改推荐排序
    public void updateBySort(Integer id, Integer sort) {
        //封装实体类
        SmsHomeRecommendSubject smsHomeRecommendSubject = new SmsHomeRecommendSubject();
        smsHomeRecommendSubject.setId(Convert.toLong(id));
        smsHomeRecommendSubject.setSort(sort);
        //调用方法修改
        smsHomeRecommendSubjectService.updateBySort(smsHomeRecommendSubject);
    }
}
